var vm = new Vue({
    el: '#contentContainer',
    data: {
        form: {
            jobId: '',
            beanName: '',
            methodName: '',
            methodParams: '',
            cronExpression: '',
            jobStatus: '',
            remark: '',
        },
        rules: {
            beanName: [
                {required: true, message: '请输入bean名称', trigger: 'blur'},
                {min: 0, max: 255, message: '长度不能超过255个字符', trigger: 'blur'}
            ],
            methodName: [
                {required: true, message: '请输入方法名称', trigger: 'blur'},
                {min: 0, max: 255, message: '长度不能超过255个字符', trigger: 'blur'}
            ],
            cronExpression: [
                {required: true, message: '请输入cron表达式', trigger: 'blur'},
                {min: 0, max: 255, message: '长度不能超过255个字符', trigger: 'blur'}
            ]
        }
    },
    mounted: function () {
        var $this = this;

        // cron表达式生成器
        $('#cronSelector').cron({
            onChange: function () {
                $this.form.cronExpression = $('#cronSelector').cron("value");
            },
            useGentleSelect: true
        });

        Common.getJson('/sys/job/getSysJobInfo',
            {jobId: Common.getUrlParam('jobId')},
            function (response) {
                if (!response || response.status != 200)
                    return;

                var sysJobInfo = response.result;
                $this.form.jobId = sysJobInfo.jobId;
                $this.form.beanName = sysJobInfo.beanName;
                $this.form.methodName = sysJobInfo.methodName;
                $this.form.methodParams = sysJobInfo.methodParams;
                $this.form.cronExpression = sysJobInfo.cronExpression;
                $this.form.jobStatus = sysJobInfo.jobStatus;
                $this.form.remark = sysJobInfo.remark;
            });
    },
    methods: {
        submitForm: function (formName) {
            var $this = this;

            $this.$refs[formName].validate(function (valid) {
                if (!valid)
                    return false;

                Common.postJson('/sys/job/edit', $this.form,
                    function (response) {
                        if (!response)
                            return;

                        //保存成功
                        if (response.status == 200 && response.result.success)
                            window.location.href = '/sys/job/list';
                        else {
                            var errMsg = response.status == 200 ? response.result.errMsg : response.msg;

                            $this.$notify.error({
                                title: '错误',
                                message: errMsg,
                                offset: $("#main", window.parent.document).height() - 120,
                                duration: 0
                            });
                        }
                    });
            });
        },
        resetForm: function (formName) {
            this.$refs[formName].resetFields();
        }
    }
});